package com.ruoyi.project.exam.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.constant.Handle;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.exam.domain.ExamModel;
import com.ruoyi.project.exam.domain.ExamPaperModel;
import com.ruoyi.project.exam.domain.QuModel;
import com.ruoyi.project.exam.domain.RepoModel;
import com.ruoyi.project.exam.service.IExamService;
import com.ruoyi.project.exam.service.IRepoService;
/**
 * 考试管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/exam/exam")
public class ExamController extends BaseController{
	
	private String prefix = "exam/exam";
	@Autowired
	private IExamService examService;
	
	@Autowired
	private IRepoService repoService;
	
	@RequiresPermissions("exam:exam:view")
	@GetMapping()
	public String exam(ModelMap mmap) {
		return prefix + "/exam";
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("exam:exam:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ExamModel model) {
		startPage();
		List<ExamModel> list = examService.selectExamList(model);
		return getDataTable(list);
	}
	
	/**
	 * 新增
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap) {
		List<RepoModel> repoList = repoService.selectAll();
		mmap.put("repoList", repoList);
		return prefix + "/add";
	}
	
	/**
	 * 新增保存
	 */
	@RequiresPermissions("exam:exam:add")
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ExamModel model) {
		try{
			examService.insertExam(model);
			return toAjax(Handle.SUCCESS.getCode());
		}catch(Exception e){
			e.printStackTrace();
		}
		return toAjax(Handle.FAIL.getCode());
	}
	/**
	 * 修改
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap) {
		mmap.put("model", examService.selectExamById(id));
		List<RepoModel> repoList = repoService.selectAll();
		mmap.put("repoList", repoList);
		return prefix + "/edit";
	}
	/**
	 * 修改保存
	 */
	@RequiresPermissions("exam:exam:edit")
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ExamModel model) {
		try{
			examService.updateExam(model);
			return toAjax(Handle.SUCCESS.getCode());
		}catch(Exception e){
			e.printStackTrace();
		}
		return toAjax(Handle.FAIL.getCode());
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("exam:exam:remove")
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(examService.deleteExamByIds(ids));
	}
	
	/**
	 * 发布
	 */
	@RequiresPermissions("exam:exam:edit")
	@PostMapping("/release")
	@ResponseBody
	public AjaxResult release(String id) {
		return toAjax(examService.releaseExamById(id));
	}
	/**
	 * 考试统计
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/examReport/{id}")
	public String examReport(@PathVariable("id") String id, ModelMap mmap) {
		mmap.put("model", examService.selectExamById(id));
		return  "exam/userExam/userExam";
	}
	
	/**
	 * 试卷预览
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/examPaperPre/{id}")
	public String examPaperPre(@PathVariable("id") String id, ModelMap mmap) {
		ExamPaperModel examPaperModel = examService.selectExamPaperPre(id);
		mmap.put("model", examPaperModel);
		return  prefix+"/paperPre";
	}
}
